<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi
 
    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

class Book extends BaseBook
{

	public function save(PropelPDO $con = null) {
		parent::save($con);		// we need to save the book first
		$this->updateLuceneIndex();  // update Lucene index
	}

	public function delete(PropelPDO $con = null)
	{
		$index = Search::getLuceneIndex(Search::BOOK);
		$term  = new Zend_Search_Lucene_Index_Term($this->getId(), 'pk');
		$query = new Zend_Search_Lucene_Search_Query_Term($term);
		$hit   = $index->find($query);
		if ($hit) {
  			foreach($hit as $h) { $index->delete($h->id); }
		}

		parent::delete($con);
	}

	public function updateLuceneIndex() {
  		$index = Search::getLuceneIndex(Search::BOOK);
 
  		// remove an existing entry
		$term  = new Zend_Search_Lucene_Index_Term($this->getId(), 'pk');
		$query = new Zend_Search_Lucene_Search_Query_Term($term);
		$hit   = $index->find($query);
  		if ($hit) {
    			foreach($hit as $h) { $index->delete($h->id); }
  		}
 
  		$doc = new Zend_Search_Lucene_Document();
 
	  	// store primary key to identify it in the search results
	  	$doc->addField(Zend_Search_Lucene_Field::Keyword('pk', $this->getId()));
 
	  	// index fields
	  	$doc->addField(Zend_Search_Lucene_Field::Text('title', stripslashes($this->getTitle())));
		$doc->addField(Zend_Search_Lucene_Field::Text('author', stripslashes($this->getAuthor())));
		if($this->getEssay()) {
			$doc->addField(Zend_Search_Lucene_Field::UnStored('essay', stripslashes($this->getEssay())));
		}
		if($this->getSummary()) {
			$doc->addField(Zend_Search_Lucene_Field::UnStored('summary', stripslashes($this->getSummary())));
		}		

		$sections = SectionPeer::retrieveByBookId($this->getId());
		if($sections) {
			$totalcontent = NULL;
			foreach($sections as $section) {
				$content = ContentPeer::retrieveBySectionId($section->getId());
				if($content) {
					if(!$totalcontent) {
						$totalcontent = stripslashes($content->getContent());
					} else {
						$totalcontent = $totalcontent . ' ' . stripslashes($content->getContent());
					}
				}
			}
			if($totalcontent) {
				$doc->addField(Zend_Search_Lucene_Field::UnStored('content', $totalcontent));
			}
		}

  		// add to the index
  		$index->addDocument($doc);
  		$index->commit();
  	}
}
